<template>
  <div class="app-container">

    <el-container>
      <el-aside width="200px" style="background-color:#fff; padding: 0">
        <el-tree style="width: 100%;margin: 0;padding: 0"
          :data="urgeList"
          :props="defaultProps"
          :default-expand-all="true"
          :highlight-current="true"
          @node-click="handleNodeClick">
        </el-tree>
      </el-aside>
      <el-container>
        <el-header style="width: 100%; background-color:#fff; padding: 0;margin-left: 10px">
          <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="商铺">
              <HouseInput v-model="queryParams.shopsCode" type="house" />
            </el-form-item>
<!--<el-form-item label="收费项">-->
<!--              <el-select v-model="queryParams.chargeItemId" placeholder="请选择收费项" clearable>-->
<!--                <el-option v-for="item in chargeList" :key="item.id" :label="item.name" :value="item.id" />-->
<!--              </el-select>-->
<!--            </el-form-item>-->
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="getChargeByBuildCode">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>

        </el-header>

        <el-main style="width: 100%; background-color:#EBEEF5;padding: 0;margin-left: 10px;margin-top: 10px">
          <el-table border v-loading="loading" :data="chargList" @selection-change="handleSelectionChange">
            <el-table-column label="序号" type="index" width="55" align="center" />
            <el-table-column label="编号" align="center" prop="shopsCode" />
            <el-table-column label="收费项名" align="center" prop="chargeItemName" />
            <el-table-column label="开始计费日期" align="center" prop="startTime" />
            <el-table-column label="结束计费日期" align="center" prop="endTime" />
            <el-table-column label="缴费限期" align="center" prop="deadlineTime" />
            <el-table-column label="状态" align="center" >
              待付款
            </el-table-column>
            <el-table-column label="金额" align="center" prop="money" />
            <el-table-column label="滞纳金" align="center" prop="latefee" />
            <el-table-column label="负责人" align="center" prop="chargeUser" />
          </el-table>
          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />


        </el-main>
      </el-container>
    </el-container>

    <!-- 添加或修改费用催缴对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUrge, getUrge, delUrge, addUrge, updateUrge,chargeList,chargeByBuildId,chargeByBuildCode } from "@/api/system/urge";

export default {
  name: "Urge",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 费用催缴表格数据
      urgeList: [],
      // 获取商业区和商铺列表
      chargList:[],
      chargeItemList:[],
      defaultProps:{
        label:'name',
        value:'id',
        children:'zhwyBuildingList',
      },
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        shopsCode:'',
      },
      //树形菜单选中处理
      selectedDep:{
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getChargeList();
  },
  methods: {
    // 根据Code查询
    getChargeByBuildCode(){
      if (!this.queryParams.shopsCode){
        this.$modal.msgError("请选择商铺");
        return;
      }
      this.loading = true;
      this.chargList = [];
      chargeByBuildCode(this.queryParams).then(response => {
        this.chargList = response.rows;
        this.loading = false;
      });
    },

    // 根据楼栋ID查询
    getChargeByBuildId(id){
      this.loading = true;
      chargeByBuildId(id).then(response => {
        this.chargList = response.data;
        this.loading = false;
      });
    },
    // 查询收费信息
    getChargeList(){
      this.loading = true;
      chargeList(this.queryParams).then(response => {
        this.chargList = response.rows;
        this.loading = false;
      });
    },

    // 树形目录点击事件
    handleNodeClick(data){
      // data 就是被点击的节点数据对象
      if (data.zhwyBuildingList && data.zhwyBuildingList.length > 0) {
        // console.log('当前点击节点的子节点数据:', data.zhwyBuildingList);
        // 这里可以处理子节点数据，比如发送请求等
      } else {
        this.chargList = [];
        this.getChargeByBuildId(data.id);
        console.log('楼栋ID:'+data.id);

      }
    },
    /** 查询菜单树列表 */
    getList() {
      this.loading = true;
      listUrge(this.queryParams).then(response => {
        this.urgeList = response.rows;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加费用催缴";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getUrge(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改费用催缴";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateUrge(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUrge(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除费用催缴编号为"' + ids + '"的数据项？').then(function() {
        return delUrge(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/urge/export', {
        ...this.queryParams
      }, `urge_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style scoped>
.app-container{
  background-color: #EBEEF5;

  padding: 10px 10px;
}


</style>
